#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/timeb.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/uio.h>


#include "rdb_lib.h"
#include "msgbus_lib.h"
#include "base_lib.h"
#include "hdb_lib.h"
#include "usleep.h"

#define INCRESS_SAFE_RUN_DAYS	120
#define ERASE_SLEEP_DISTANCE    2
#define	ERASE_TIME		1200//20min

#define	ONE_DAY_SECONDS		86400
#define	ONE_HOUR_SECONDS	3600
#define DIS_BETWEEN_FRAME	1

int execute_sql_sentence(char *sql_sentence);
extern int my_host_are_online(class system_net_info &sys_info);
extern class 	system_net_info  history_sample;

int incress_safe_run_days()
{
	char sql_sentence[MAX_SQL_SENTENCE_LEN];

	strcpy(sql_sentence,"update fac_info set safe_day=safe_day+1 ");
	execute_sql_sentence(sql_sentence);

	return 1;
}

int update_safe_run_days()
{
    char sql_sentence[MAX_SQL_SENTENCE_LEN];

    strcpy(sql_sentence,"update fac_info set safe_day = 0 where safe_day is null ");
    execute_sql_sentence(sql_sentence);//ok

	strcpy(sql_sentence,"update cb_device set bw_number_value = 0 where bw_number_value is null ");
	execute_sql_sentence(sql_sentence);//ok

    return 1;
}

int move_data_from_one_table_to_another(char *src_table_name, char *dest_table_name)
{
    char sql_sentence[MAX_SQL_SENTENCE_LEN];
	sprintf(sql_sentence,"truncate table %s ", dest_table_name);
	execute_sql_sentence(sql_sentence);
	
	sprintf(sql_sentence,"insert into %s ", dest_table_name);
	strcat(sql_sentence,"select id -1000000,time_dot ,");
	strcat(sql_sentence,"plan_01,plan_02,plan_03,plan_04,plan_05,plan_06,plan_07,plan_08,plan_09,plan_10,");
	strcat(sql_sentence,"plan_11,plan_12,plan_13,plan_14,plan_15,plan_16,plan_17,plan_18,plan_19,plan_20,");
	strcat(sql_sentence,"plan_21,plan_22,plan_23,plan_24,plan_25,plan_26,plan_27,plan_28,plan_29,plan_30,");
	strcat(sql_sentence,"plan_31,plan_32,plan_33,plan_34,plan_35,plan_36,plan_37,plan_38,plan_39,plan_40,");
	strcat(sql_sentence,"plan_41,plan_42,plan_43,plan_44,plan_45,plan_46,plan_47,plan_48,plan_49,plan_50,");
	strcat(sql_sentence,"plan_51,plan_52,plan_53,plan_54,plan_55,plan_56,plan_57,plan_58,plan_59,plan_60,");
	strcat(sql_sentence,"plan_61,plan_62,plan_63,plan_64,plan_65,plan_66,plan_67,plan_68,plan_69,plan_70,");
	strcat(sql_sentence,"plan_71,plan_72,plan_73,plan_74,plan_75,plan_76,plan_77,plan_78,plan_79,plan_80,");
	strcat(sql_sentence,"plan_81,plan_82,plan_83,plan_84,plan_85,plan_86,plan_87,plan_88,plan_89,plan_90,");
	strcat(sql_sentence,"plan_91,plan_92,plan_93,plan_94,plan_95,plan_96,plan_97,plan_98,plan_99,plan_100 ");
	strcat(sql_sentence," from ");
	strcat(sql_sentence, src_table_name);
    execute_sql_sentence(sql_sentence);//ok
	return 1;
}

int process_plan() //98.12.11
{
    char sql_sentence[MAX_SQL_SENTENCE_LEN];
	move_data_from_one_table_to_another("plan_tomorrow", "plan_today");
	move_data_from_one_table_to_another("plan_2days_later", "plan_tomorrow");
	move_data_from_one_table_to_another("plan_3days_later", "plan_2days_later");
	move_data_from_one_table_to_another("plan_4days_later", "plan_3days_later");
	move_data_from_one_table_to_another("plan_5days_later", "plan_4days_later");
	move_data_from_one_table_to_another("plan_6days_later", "plan_5days_later");
	move_data_from_one_table_to_another("plan_7days_later", "plan_6days_later");
	move_data_from_one_table_to_another("plan_8days_later", "plan_7days_later");

	strcpy(sql_sentence,"update plan_flag set load_flag=0 ");
	execute_sql_sentence(sql_sentence);
	printf("PLAN_FLAG:%s\n",sql_sentence);
	TRACE_INFO("PLAN_FLAG:%s\n",sql_sentence);
//end
    return 1;
}

void delete_his_data()
{
	time_t now;
	time(&now);
	struct  tm      *st_time;
    	st_time=localtime((const time_t*)&now);
	int cur_year=st_time->tm_year+1900;
	int cur_month=st_time->tm_mon+1;

	FILE	*fp; 
	char	filename[100], str[1024];
	char	*ret;
	int last_year = 2001;
	int last_month = 1;
	strcpy(filename, "../sys/his_start_month.sys");
	fp = fopen(filename, "r");
	if( fp )
	{
		ret = fgets( str, 1024, fp );
		if( ret != NULL) {
			sscanf(str, "%d %d", &last_year, &last_month);
		}
		fclose(fp);
	}

	int month_dis = (cur_year-last_year)*12+cur_month-last_month;
	if( month_dis > 12 )
	{
		last_year = cur_year - 1;
		last_month = cur_month - 1;
		if(last_month <=0)
		{
			last_month += 12;
			last_year --;
		}
		sprintf(str, "export_tables -month %04d%02d &", last_year, last_month);
		printf("cmd: %s\n", str);

		system(str);


		last_year = cur_year - 1;
		last_month = cur_month;
		sprintf(str, "\"../sql_sentence/rm_his\" 20010101 %04d%02d01 &", last_year, last_month);
		printf("cmd: %s\n", str);

		system(str);

		fp = fopen(filename, "w");
		if( fp )
		{
			fprintf(fp, "%d %d\n", last_year, last_month);
		}
		fclose(fp);
	}

}

int 	copy_and_erase_noused_fun()
{
	int	retcode,num;
	time_t	current_time;
	time_t  current_time_at_hour_point;
	static time_t  last_erase_time=0;
	static time_t  last_incress_safe_day=0;
	time_t	current_day;
	int	time_offset,time_offset_day,i;

	{

		time(&current_time);
		time_offset = current_time % ONE_HOUR_SECONDS;
		time_offset_day = current_time % ONE_DAY_SECONDS; 
		current_day = current_time / ONE_DAY_SECONDS;
		current_time_at_hour_point = current_time / ONE_HOUR_SECONDS *ONE_HOUR_SECONDS;
		if (current_day != last_incress_safe_day &&
			time_offset_day >=INCRESS_SAFE_RUN_DAYS &&
			time_offset_day<INCRESS_SAFE_RUN_DAYS+ERASE_SLEEP_DISTANCE*6)
		{
			last_incress_safe_day = current_day;
			if (my_host_are_online(history_sample) == DNET_YES) 
			{ 
				incress_safe_run_days();
				process_plan(); 
			}

			delete_his_data();
		}
	
	}
 



	return(0);
}

